<?php
namespace app\admin\controller\openservice;
use app\common\controller\Admin;
use app\common\annotation\NodeAnnotation;
use app\common\traits\ControllerCurd;
use think\Request;

/**
 * Class Index
 * @NodeAnnotation(
 *     title="用户服务配置控制器",
 *     parent="admin/openservice.index",
 *     is_menu=0,
 *     is_view=0,
 *     auth=1
 * )
 */
class Userapp extends Admin
{
    use ControllerCurd;
    public $model;
    public $uid;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\openservice\Userapp();
        $this->uid   = $this->request->param("uid",0,'intval');

        $this->assign([
            "uid" => $this->uid
        ]);
    }

    /**
     * @NodeAnnotation(
     *     title="用户服务列表",
     *     parent="admin/openservice.userapp",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function index()
    {
        $whre = [];

        if($this->uid!=0) array_push($whre,['uid','eq',$this->uid]);

        $keywords = $this->request->param('keywords');

        if($keywords)
        {
            $app = (new \app\admin\model\openservice\Openapps())
                ->where('id|title',$keywords)
                ->column('id');
            array_push($whre,['group_id','in',$app]);
        }


        $userAppList = $this->model->where($whre)
            ->with('user_app')
            ->paginate();

        $this->assign([
            "userAppList" => $userAppList,
            "search"      => [
                "keywords"=> $keywords
            ]
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加用户服务",
     *     parent="admin/openservice.userapp",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function adduserapp(Request $request)
    {
        if($request->isAjax())
        {
            $dataType = $request->param("dataType");
            if($dataType == 'Apps')
            {
                $appList = (new \app\admin\model\openservice\Openapps())->field('id,title')->select();
                return json($appList);
            }
            $app_id = $request->param("app_id",0,'intval');

            if($app_id)
            {
                $ruleList = (new \app\admin\model\openservice\Openapps())
                    ->field("rules")
                    ->find($app_id);

                $apiList = (new \app\admin\model\openservice\OpenServiceApi())
                    ->whereIn("id",$ruleList['rules'])
                    ->field("id,title,parent_id")
                    ->select();

                return json($apiList);
            }
        }
        $this->add(url('admin/openservice.userapp/index',['uid'=>$this->uid]));
        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="编辑用户服务",
     *     parent="admin/openservice.userapp",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function edituserapp(Request $request,$id=0)
    {
        if($request->isAjax())
        {
            $dataType = $request->param("dataType");
            $appkey  = $request->param("appkey",0,'intval');

            if($dataType == 'Apps')
            {
                $appList = (new \app\admin\model\openservice\Openapps())->field('id,title')->select();
                foreach ($appList as  $k=>$v)
                {
                    if($v['id']==$appkey) $appList[$k]['checked'] = true;
                }
                return json($appList);
            }

            $app_id = $request->param("app_id",0,'intval');

            if($app_id)
            {
                $ruleList = (new \app\admin\model\openservice\Openapps())
                    ->field("rules")
                    ->find($app_id);

                $apiList = (new \app\admin\model\openservice\OpenServiceApi())
                    ->whereIn("id",$ruleList['rules'])
                    ->field("id,title,parent_id")
                    ->select();

                if($ruleList)
                {
                    $role['rules'] = explode(',',$ruleList['rules']);
                    foreach ($apiList as $k=>$item)
                    {
                        if(in_array($item['id'],$role['rules']))
                        {
                            $apiList[$k]['checked'] = true;
                        }else{
                            $apiList[$k]['checked'] = false;
                        }
                    }
                }
                return json($apiList);
            }
        }

        $userapp = $this->model->find($id);

        $this->edit(url('admin/openservice.userapp/index',['uid'=>$userapp['uid']]));

        $this->assign([
            "userapp" => $userapp
        ]);

        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="编辑用户服务",
     *     parent="admin/openservice.userapp",
     *     MenuRoot="admin/openservice.index",
     *     is_menu=0
     * )
     */
    public function deluserapp(Request $request)
    {
        $this->del();
    }
}